package app.auto.runner.base;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.widget.TextView;

import com.aliang.auto.R;

import java.util.List;

import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.ListBuilder;

public class Spilt2Util {
    public static String[] splitAlpha(String text, List range, boolean settext, TextView t1, TextView t2, FunCallback funCallback) {

        String[] headt = text.replaceAll(" ", "").split("\\|");
        String h = headt[0].substring(0, 1);
        String t = headt[headt.length - 1].substring(headt[headt.length - 1].length() - 1);

        String splitX = "";
        int hi = range.indexOf(h);
        int ti = range.indexOf(t);
        int count = ti - hi + 1;
        int midi;
        if (count % 2 == 0) {
            midi = count / 2;
        } else {
            midi = count / 2;
        }
        String smidi = (String) range.subList(hi, ti + 1).get(midi);
        String smidiPre;
        if (range.subList(hi, ti + 1).size() == 1) {
            smidiPre = smidi;
        } else {
            smidiPre = (String) range.subList(hi, ti + 1).get(midi - 1);
        }

        String[] splitGroup = new String[]{
                h + "  - " + smidiPre, smidi + " -  " + t
        };
        SpannableStringBuilder builder1 = new SpannableStringBuilder(splitGroup[0]);

        builder1.setSpan(new AbsoluteSizeSpan(1), splitGroup[0].length() - 1, splitGroup[0].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableStringBuilder builder2 = new SpannableStringBuilder(splitGroup[1]);

        builder2.setSpan(new AbsoluteSizeSpan(1), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (t1.getText().toString().equals(builder1.toString()) && t2.getText().toString().equals(builder2.toString())) {
            return new String[2];
        }
        t1.setText(builder1);
        t2.setText(builder2);

            t1.setTag(R.id.tag_data, range.subList(hi, hi+midi));
            t2.setTag(R.id.tag_data, range.subList(hi+midi, ti));

        return splitGroup;
    }

}
